package com.hzqy.service.epg;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.hzqy.commons.utils.WChatUtils;
import com.hzqy.persistence.dao.epg.AcIptvListDao;
import com.hzqy.web.ac.epg.epgCommonAction;
import com.hzqy.web.vo.epg.AcIptvListVo;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;


/*
 * 环球现网服务器使用
 */
@Service
public class AcIptvListServiceBean {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private AcIptvListDao iptvDao;
	//查询总条数
	public int selectCountActivityid(AcIptvListVo ailv){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ailv.setF_iptv_date(sdf.format(new Date()));
		return iptvDao.selectCountActivityid(ailv);
	}
	//查询总条数插入
	public int insertIptvList(AcIptvListVo ailv){
		return iptvDao.insertIptvList(ailv);
	}
	/**
	 * 3.16积分产生同步
	 * @param request
	 * @return
	 */
	public Map<String, Object> pointsProduce(String iptvAccount,String activityCode,
			String extraCode,String remark,int points,String activityid ) {
		String secretKey =epgCommonAction.getEPGSecretKey(iptvAccount);//接口调用密钥后台生成
		String serverPath = WchatService.getDomain();
		String QRCodeUrl = serverPath+"/mobile/iptv/pointsProduce.do?secretKey="+secretKey+"&iptvAccount="+iptvAccount
				+"&activityCode="+activityCode+"&extraCode="+extraCode+"&points="+points;
		String iptv = WChatUtils.sendGet(QRCodeUrl);
		JSONObject resultJson = JSONObject.parseObject(iptv);
		LOG.debug("积分产生同步：userId:"+iptvAccount+",结果"+resultJson);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", resultJson.get("data"));
		map.put("message", resultJson.get("message"));
		map.put("error", resultJson.get("error"));
		map.put("status", resultJson.get("status"));
		//本地备份数据记录
		AcIptvListVo ailv = new AcIptvListVo();
		ailv.setF_iptv_serverid(iptvAccount);
		ailv.setF_iptv_activityid(activityid);
		ailv.setF_iptv_desc(remark);
		ailv.setF_iptv_stats(1);
		ailv.setF_iptv_votenum(points);
		int id = insertIptvList(ailv);
		return map;
	}
	/**
	 * （活动）同步奖品
	 * @param secretKey
	 * @param iptvAccount
	 * @return
	 * @throws IOException
	 */
	public Map<String, Object> addAward(String iptvAccount,String activityCode,String goodsCode) {
		AcIptvListVo ailv = new AcIptvListVo();
		String serverPath = WchatService.getDomain();
		String secretKey =epgCommonAction.getEPGSecretKey(iptvAccount);//接口调用密钥后台生成
		String QRCodeUrl = serverPath+"mobile/iptv/addAward.do?iptvAccount="+iptvAccount+"&activityCode="+activityCode+"&goodsCode="+goodsCode+"&secretKey="+secretKey;
		LOG.debug("同步奖品URL--userId:"+iptvAccount+",QRCodeUrl:"+QRCodeUrl);
		JSONObject resultJson=null;
		try {
			String iptv = WChatUtils.sendGet(QRCodeUrl);
			resultJson = JSONObject.parseObject(iptv);
		} catch (Exception e) {
			LOG.error("同步奖品-请求微信服务器error:"+e);
		}
		LOG.debug("同步奖品userId:"+iptvAccount+",结果"+resultJson);
		Map<String, Object> map = new HashMap<String, Object>();
		if (resultJson!=null && "200".equals(resultJson.get("status").toString())) {
			map.put("message", resultJson.get("message"));
			map.put("status", resultJson.get("status"));
			map.put("data", resultJson.get("data"));
			ailv.setF_iptv_desc("同步奖品成功,goodsCode="+goodsCode);
		}else {
			map.put("message", "error");
			map.put("status", "500");
			ailv.setF_iptv_desc("同步奖品失败,goodsCode="+goodsCode);
		}
		//本地备份数据记录
		ailv.setF_iptv_serverid(iptvAccount);
		ailv.setF_iptv_activityid(activityCode);
		ailv.setF_iptv_stats(1);
		ailv.setF_iptv_votenum(0);
		insertIptvList(ailv);
		return map;
	}
}
